package com.admin.service;

import com.admin.dbdao.SoftWareClassifyDao;
import com.data.bean.Menu;
import com.data.util.CheckDataUtils;
import com.data.util.DatabaseIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 软件分类服务层
 *
 * @author:HingLo
 * @create 2017-08-29 9:53
 **/
@Service
public class SoftWareClassifyService {

    @Autowired
    private SoftWareClassifyDao softWareClassifyDao;

    /**
     * 添加分类
     *
     * @param menu
     * @param file
     * @return
     */
    public boolean addClassService(Menu menu, MultipartFile file) {
        String name = menu.getName();
        if (CheckDataUtils.stringUtils(name)) {
            if (!CheckDataUtils.stringUtilsID(menu.getId())) {
                menu.setId(DatabaseIdUtils.getDataIdOne());
            }

            try {
                menu.setIcon(file.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return softWareClassifyDao.saveSoftWareClassDao(menu);
        }
        return false;
    }

    /**
     * 默认查找软件的一级菜单
     *
     * @return
     */
    public List<Menu> findMenuOneService() {
        return (List<Menu>) softWareClassifyDao.findMenuOneDao();
    }

    /**
     * 根据参数来查找一级菜单
     *
     * @param parm
     * @return
     */
    public List<?> findMenuOneService(int parm) {
        return softWareClassifyDao.findMenuOneDao(parm);
    }

    /**
     * 查看分类
     *
     * @return
     */
    public List<Menu> lookClassService() {
        return (List<Menu>) softWareClassifyDao.lookClassDao();
    }

    /**
     * 根据id来查询类别信息
     *
     * @param id
     * @return
     */
    public Menu lookOneClassService(String id) {

        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            return softWareClassifyDao.lookOneClassDao(id);
        }

        return new Menu();
    }

    /**
     * 通过id来更新信息
     *
     * @param id
     * @param name
     * @return
     */
    public boolean changeClassService(String id, String name) {
        id = id.trim();
        name = name.trim();
        return softWareClassifyDao.changeClassDao(id, name);
    }

    /**
     * 根据id来查二级菜单的信息
     *
     * @param fid
     * @return
     */
    public List<?> findMenuTwoService(String fid, int type) {
        fid = fid.trim();
        return softWareClassifyDao.findMenuTwoDao(fid, type);
    }
}
